package com.example.book.controller;

import com.example.book.Service.BookService;
import com.example.book.constants.Constants;
import com.example.book.model.*;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookService bookService;
    @RequestMapping("/getBookList")
    public Result<PageResult<BookInfo>> getBookList(PageRequest pageRequest, HttpSession session) {
//        //获取用户信息
//        UserInfo userInfo = (UserInfo)session.getAttribute(Constants.USER_SESSION_KEY);
//
//        //检查用户是否登录
//        if(userInfo == null) {
//            return Result.noLogin();
//        }

        //成功
        PageResult<BookInfo> bookList = bookService.getBookList(pageRequest);
        return Result.success(bookList);
    }

    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        //校验参数
        log.info("BookController.addBook():接收到参数:bookInfo{}", bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null) {
            return "输入数据不全";
        }
        //添加图书
        try{
            bookService.addBook(bookInfo);
        }catch(Exception e) {
            log.error("添加图书异常: ", e);
            return "添加图书失败";
        }
        return "";
    }
    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId) {
        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("接收到参数：{}", bookInfo);
        try {
            Integer result = bookService.updateBook(bookInfo);
            if(result != 0) {
                return "";
            }else{
                return "内部错误";
            }
        } catch (Exception e) {
            log.error("修改失败：", e);
            return "修改失败";
        }
    }

    @RequestMapping(value = "/deleteBook", produces = "application/json")
    public String deleteBook(Integer bookId) {
        BookInfo bookInfo = new BookInfo();
        bookInfo.setId(bookId);
        bookInfo.setStatus(0);
        try {
            Integer result = bookService.deleteBook(bookInfo);
            if(result != 0) {
                return "";
            }else{
                return "内部错误";
            }
        } catch (Exception e) {
            log.error("删除失败：", e);
            return "删除失败";
        }
    }

    @RequestMapping(value = "/batchDelete", produces = "application/json")
    public String batchDelete(@RequestParam(name = "ids") List<Integer> ids) {
        log.info("接收到参数ids:{}", ids);
        try {
            Integer result = bookService.batchDelete(ids);
            if(result != 0) {
                return "";
            }else{
                return "内部错误";
            }
        } catch (Exception e) {
            log.error("删除失败：", e);
            return "删除失败";
        }
    }
}
